package com.mall.admin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import com.jfinal.log.Log;
import com.jfinal.upload.UploadFile;
import com.mall.admin.config.SysConfig;
import com.mall.common.constant.CommonConstant;
import com.mall.common.controller.BaseController;
import com.mall.common.dto.UserSession;
import com.mall.common.model.system.SystemOperLog;
import com.mall.common.util.DateUtil;
import com.mall.common.util.IpUtils;
import com.mall.common.util.NumberUtils;

public abstract class AdminBaseController extends BaseController{

    private static final Log LOG = Log.getLog(BaseController.class);
	
	protected  int pageSize = 30;
	
	public abstract void index();
	
	public void renderView(String path){
		render(SysConfig.BASE_VIEW + path);
	}
	
    /**
     * 获取在线用户信息
     * @return
     */
	public  UserSession getCurrentUser(){
		return  getSessionAttr(CommonConstant.SESSION_ID_KEY);
	}
	
	/**
	 * 获取登录用户名
	 * @return
	 */
	public String getUserName(){
		return getCurrentUser().getLoginName();
	}
	
	public Integer getUserId(){
		return getCurrentUser().getUserId();
	}
	
	/**
	 * 系统日志记录
	 * @param oper_des
	 * @param type
	 */
	@SuppressWarnings("static-access")
	public void systemLog(String oper_des,int type){
		UserSession user = getCurrentUser();
		SystemOperLog systemLog = new SystemOperLog();
		systemLog.set("oper_name", user.getLoginName());
		systemLog.set("user_id", user.getUserId());
		systemLog.set("oper_time", new DateUtil().getDate());
		systemLog.set("oper_ip", IpUtils.getAddressIp(getRequest()));
		systemLog.set("login_type",type);
		systemLog.set("oper_desc",oper_des);
		systemLog.save();
	}
	
	
	public HttpSession getSession(){
		return getRequest().getSession();
	}
	
	/**
	 * 文件上传重命名
	 * @param upload
	 * @return
	 */
	public String uploadRename(UploadFile upload){
		File file = upload.getFile();
		try {
			FileInputStream in = new FileInputStream(file);
			String fileName = upload.getFileName();
			String style = fileName.substring(fileName.indexOf("."),fileName.length());
			String newName = NumberUtils.getNumberCode(4) + style;
			String imagePath = getImagePath();
			String basePath = SysConfig.resourceUpload + "/" + imagePath;
			File fi = new File(basePath);
			if(!fi.exists()){
				fi.mkdirs();
			}
			File upFile = new File(basePath,newName);
			FileOutputStream out = new FileOutputStream(upFile);
			byte[] bytes = new byte[1024];
			int flag = 0;
			while((flag = in.read(bytes, 0, 1024)) != -1){
				out.write(bytes, 0, flag);
			}
			if(out != null){
				out.close();
			}
			if(in != null){
				in.close();
			}
			file.delete();
			return imagePath + "/" + newName;
		} catch (Exception e) {
			LOG.error("文件上传异常",e);
		}
		return null;
	}

	/**
	 * 生成新的图片文件
	 * @return
	 */
	private String getImagePath(){
		DateFormat format = new SimpleDateFormat("yyyy-MMdd");
		return format.format(new Date()).replaceAll("-", "/");
	}
	
	/**
	 * 发送站内信
	 * @param userId
	 * @param title
	 * @param content
	 */
	public void sendMessage(int userId,String title,String content){
		
	}
}
